<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="/common/login-info.jsp"%>

<html>
	<head>
		<title>流程发布管理</title>
		<%@ include file="/common/meta.jsp"%>
		<%@ include file="/common/jquery-ui-inc.jsp"%>
		<script>
$.ajaxSetup({
	cache:false
})
$(function() {
    var  dataListUrl = '';  
    if("operator" == "${operType}"){
         dataListUrl = '${ctx}/flow/core/flow-deploy!flowData.action?operType=${operType}';  
    }else{
        dataListUrl = '${ctx}/flow/core/flow-deploy!flowData.action?orgId=<%=loginUser.getOrgId()%>'; 
    }   
	$('#lockList').datagrid( {
		title : '流程定义',
		url : dataListUrl,
		loadMsg : '数据载入中，请稍后……',
		nowrap : false,
		collapsible : false,
		fitColumns : true,
		pagination : true,
		singleSelect : true,
		rownumbers : true,
		remoteSort : false,
		pageList : [ 15, 30, 50 ],
		idField : 'dbId',
		onDblClickRow: function() { 
    		var node = $('#lockList').datagrid('getSelected'); 
   			if (node){ 
   				if(node.processStatus!='1'){
   					window.location.href="${ctx}/flow/core/flow-deploy!viewPic.action?deploymentId="+node.processDeploymentId;
   				}
      			
			} 
		}, 

		columns : [ [ {
			field : 'sysName',
			title : '系统名称',
			width : 300,
			sortable:true,
			sorter : function(a, b) {
				return (a > b ? 1 : -1);
			}
		},{
			field : 'processDefinitionId',
			title : '流程定义Id',
			width : 200,
			sorter : function(a, b) {
				return (a > b ? 1 : -1);
			}
		}, {
			field : 'processName',
			title : '流程名',
			width : 250,
			sorter : function(a, b) {
				return (a > b ? 1 : -1);
			}
		}
//		, {
//			title : '流程发布Id',
//			field : 'processDeploymentId',
//			width : 150
//		}
		, {
			title : '流程描述',
			field : 'processSpare',
			width : 200
		}, {
			title : '流程状态',
			field : 'processStatus',
			width : 130,
			formatter: function(val, rec,rowIndex){
				if(val == '1')return "<span style='color:blue;'>未发布</span>";
				if(val == '2')return "<span style='color:red;'>已挂起</span>";
				if(val == '3')return "<span style='color:green;'>已发布</span>";
			}
		},
		<%Object operType = request.getAttribute("operType");
		if(operType!=null && "operator".equals(operType)){ %>
		{
			field : 'orgCname',
			title : '所属机构',
			width : 200
		},
		<% }%>
		{
			field : 'createTime',
			title : '创建时间',
			width : 300
		}, {
			title : '流程操作',
			field : 'cz',
			width : 300,
			formatter:function(value,row,index){
				var returninfo= '<a href="javascript:delProcessDefinition(\''+row.processDeploymentId+'\',\''+row.dbId+'\',\''+row.processStatus+'\',\''+row.processDefinitionId+'\')">删除</a>'
			 	if(row.processStatus=='3'){
			 		returninfo+='		<a href="javascript:exportXml(\''+row.processDeploymentId+'\',\''+row.fileName+'\')">导出</a>'    
			 		returninfo+='		<a href="javascript:suspend(\''+row.processDeploymentId+'\',\''+row.dbId+'\')">挂起</a>'    
			 		returninfo+='		<a href="javascript:designProcessPic(\''+row.processDeploymentId+'\',\''+row.processStatus+'\',\''+row.dbId+'\',\''+row.orgId+'\',\''+row.sysEname+'\')">更新</a>';
			 	}else if(row.processStatus=='2'){
			 	    returninfo+='		<a href="javascript:exportXml(\''+row.processDeploymentId+'\',\''+row.fileName+'\')">导出</a>'    
			 	    returninfo+='		<a href="javascript:resume(\''+row.processDeploymentId+'\',\''+row.dbId+'\')">恢复</a>'
			 	    returninfo+='		<a href="javascript:designProcessPic(\''+row.processDeploymentId+'\',\''+row.processStatus+'\',\''+row.dbId+'\',\''+row.orgId+'\',\''+row.sysEname+'\')">更新</a>';
			 	}else if(row.processStatus=='1'){
			 		returninfo+='		<a href="javascript:toSet(\''+row.fileName+'\',\''+row.pictureName+'\',\''+row.dbId+'\',\''+row.orgId+'\',\''+row.sysEname+'\')">设置</a>';
			 	}
			 	return returninfo;
			 	
			}
		}] ],

		toolbar : [ 
		 {
				text : '流程发布',
				iconCls : 'icon-add',
				handler : function() {
					deployBuProcess();
					reload(); 
				}
			}
		  ]
	});
	
	initOrgTree();
	
	var info='${request.info}';
	if(info!=""){
		$.messager.alert('提示',info, 'info');
	}
	
});


		function initOrgTree(){
		 		 $('#orgTree').combotree({//初始化机构树
		 		 //获取数据URL
				   url:'${ctx}/flow/core/flow-deploy!orgTreeJson.action',
				   valueField:'id',
				   textField:'text',
				   panelHeight:'auto',
				   onSelect:function(node){
				    /*  //返回树对象  
				     var tree = $(this).tree;  
				     //选中的节点是否为叶子节点,如果不是叶子节点,清除选中
				     var isLeaf = tree('isLeaf', node.target);  
				     if (!isLeaf) {  
				      //清除选中  
				      $('#orgTree').combotree('clear'); 
				     }  */
				   } 
				 });
			}


	//设置流程
	function designProcessPic(id,state,deployId,orgId,sysEname){
		if(state!="2"){
			$.messager.alert('提示',"请先挂起该流程！", 'warning');
			return ;
		}else{
			var url="${ctx}/flow/core/flow-deploy!toupdate.action?deploymentId=" + id+"&deployId="+deployId+"&operType=${operType}"+"&orgId="+orgId+"&sysEname="+sysEname;
			this.location=url;
		}
		 
	}
	//发布业务流程
	function deployBuProcess(){
		$('#businessProWin').window('open');
		var url = '${ctx}/flow/core/flow-deploy!toupload.action?operType=${operType}';
		$("#businessProFrame").attr("src",url);
	}
	
	function toSet(uploadFileName,imgUploadFileName,deployId,orgId,sysEname){//--
		var param = "uploadFileName="+uploadFileName+"&imgUploadFileName="+imgUploadFileName+"&deployId="+deployId+"&operType=${operType}"+"&orgId="+orgId+"&sysEname="+sysEname;
			
		window.location.href="${ctx}/flow/core/flow-deploy!design.action?"+param;
	}
	
	function closeBusProcessWin(){
		$('#businessProWin').window('close');
	}
	
	function strarProcess(id){
		$("#startProcessWin").window('open');
		$("#definitionId").val(id);
	}
	//恢复流程定义
	function resume(id,dbid){
		$.ajax({
			type : "post",
			url : "${ctx}/flow/core/flow-deploy!resume.action?deploymentId="+ id+"&deployId="+dbid,
			dataType:'json',
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data, textStatus) {
				var f = data.flag;
				if(f == "T"){
					$.messager.alert('结果', data.msg, 'info',function(){
						reload();
					});
				}else{
					$.messager.alert('错误', data.msg, 'error');
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
				//请求出错处理
			}
		});
	}
	
	//暂停和挂起流程定义
	function suspend(id,dbid){
		$.ajax({
			type : "post",
			url : "${ctx}/flow/core/flow-deploy!suspend.action?deploymentId="+ id+"&deployId="+dbid,
			dataType:'json',
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data, textStatus) {
				var f = data.flag;
				if(f == "T"){
					$.messager.alert('结果', data.msg, 'info',function(){
						reload();
					});
				}else{
					$.messager.alert('错误', data.msg, 'error');
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
				//请求出错处理
			}
		});
	}
	
	//删除流程定义
	function delProcessDefinition(id,dbid,status,processDefinitionId) {
			$.messager.confirm('确认', '您确定要删除:<font color="red">' + processDefinitionId + '</font> ？',function(r){
				if (r) {
							$.ajax( {
								type : "post",
								url : "${ctx}/flow/core/flow-deploy!delete.action?deploymentId="+ id+"&deployId="+dbid+"&status="+status,
								beforeSend : function(XMLHttpRequest) {
								},
								success : function(data, textStatus) {
									reload(); 
								},
								complete : function(XMLHttpRequest, textStatus) {
							},
							error : function() {
								//请求出错处理
							}
							});
						}
			});
	
	}
	
	//导出流程定义文件
	function exportXml(deploymentId,fileName){
		window.location.href="${ctx}/flow/core/flow-deploy!exportXml.action?deploymentId="+ deploymentId+"&uploadFileName="+fileName;
	
	}

	function closeStartProWin(){
		$('#startProcessWin').window('close');
	}
	
	function reload(){
		$('#lockList').datagrid('reload');
	}

		
	function lockQuery(){					
	      var queryParams = $('#lockList').datagrid('options').queryParams;
	      var opt = $('#lockList').datagrid('options'); 
	      $('#lockList').datagrid('options').pageNumber=1;
	      queryParams.orgId = $("[name='orgId']:input").val();
	      reload(); 
	      var page = $('#lockList').datagrid('getPager');
		  page.pagination({pageNumber:1,pageSize:opt.pageSize});
	  }
	function clearQuery(){	
		     var t = $('#orgTree').combotree('tree');//得到树对象  
		     var n = t.tree('getSelected'); // 得到选择的节点  
		     if(null != n){
		        $('#orgTree').combotree('clear');  //清除选中  
		     }
		    $("[name='orgId']:input").val('');
		    lockQuery(); 
	  }  
	  
			
	</script>
	</head>

	<body>

    <%if(operType!=null && "operator".equals(operType)){ %>
     <div title="查询" id="seaFlowDeployDiv"  class="easyui-panel" icon="icon-search"  collapsible="true">
	 <form action="" id="eform" name="efrom" method="post">
        <table class="info"  cellpadding="0" cellspacing="1" id="seaFlowDeploy">
               <tr>                   
				<td>机构名称：</td>
				<td>
					<%-- <select id="orgSelect" name="orgId" onchange="saveSecondVal()">
						<option value="">-请选择-</option>
						<s:iterator value="orgList" var="o">
							<option value="${o.orgId}">${o.orgCname}</option> 
						</s:iterator>
					</select> --%>
					
					<span id="orgspan">
							<input id="orgTree" name="orgId" class="easyui-combotree"  style="width:300px;" />
					</span>
				</td>
                <td>&nbsp;<a class="easyui-linkbutton" iconCls="icon-search" onClick="lockQuery()">查 询</a>&nbsp;<a class="easyui-linkbutton" iconCls="icon-cut" onClick="clearQuery();">清空</a></td>          
                 </tr>  
            </table>
        </form>
        </div>
         <% }%>


		<table id="lockList" style="overflow:hidden;"></table>

	
		
		<div id="businessProWin" class="easyui-window" title="发布业务流程资料"
			style="padding: 5px; width:500px; height:240px;"
			iconCls="icon-search" closed="true" maximizable="false"
			minimizable="false" collapsible="false">
			<iframe frameborder="0" id="businessProFrame" height="100%" width="100%"
				scrolling="yes" frameborder="0"></iframe>
		</div>
		
		<div id="setProWin" class="easyui-window" title="设置流程"
			style="padding: 5px; width:700px; height:540px;"
			iconCls="icon-search" closed="true" maximizable="false"
			minimizable="false" collapsible="false">
			<iframe frameborder="0" id="setProFrame" height="100%" width="100%"
				scrolling="yes" frameborder="0"></iframe>
		</div>
		
		
		<div id="startProcessWin" class="easyui-window" title="启动流程"
			style="padding: 5px; width: 330px; height:150px;"
			iconCls="icon-search" closed="true" maximizable="false"
			minimizable="false" collapsible="false" modal="true">
			<div class="easyui-layout" fit="true" border="false">
				<div region="center">
				<table class="datagrid-body">
					<tr>
						<td class="datagrid-header">
							流程定义Id
						</td>
						<td>
							<input name="definitionId" id="definitionId" readonly="readonly"/>
						</td>
					</tr>
					
					<tr>
						<td class="datagrid-header">
							流程启动参数
						</td>
						<td>
							<input name="params" id="params" value=""/>
						</td>
					</tr>
					<tr>
						<td>
							<a class="easyui-linkbutton" iconCls="icon-add"
								onclick="strarProcessOk();">启动流程</a>
						</td>
						<td>
							<a class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeStartProWin();">取消</a>
						</td>
					</tr>
				</table>
				</div>
			</div>
		</div>

	</body>
</html>